package com.bdmc.report.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.report.domain.ReportPersonMiles;
import com.bdmc.report.domain.bo.ReportPersonMilesBo;
import com.bdmc.report.domain.vo.ReportPersonMilesVo;
import com.bdmc.report.mapper.ReportPersonMilesMapper;
import com.bdmc.report.service.IReportPersonMilesService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工作时间距离统计Service业务层处理
 *
 * @author bdmc
 * @date 2022-08-11
 */
@Service
public class ReportPersonMilesServiceImpl extends ServicePlusImpl<ReportPersonMilesMapper, ReportPersonMiles, ReportPersonMilesVo> implements IReportPersonMilesService {

    @Override
    public ReportPersonMilesVo queryById(Long PersonMilesId){
        return getVoById(PersonMilesId);
    }

    @Override
    public TableDataInfo<ReportPersonMilesVo> queryPageList(ReportPersonMilesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportPersonMiles> lqw = buildQueryWrapper(bo);
        Page<ReportPersonMilesVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<ReportPersonMilesVo> mySelectPage(ReportPersonMilesBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<ReportPersonMilesVo> queryList(ReportPersonMilesBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<ReportPersonMiles> buildQueryWrapper(ReportPersonMilesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportPersonMiles> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPersonId() != null, ReportPersonMiles::getPersonId, bo.getPersonId());
        lqw.eq(bo.getMiles() != null, ReportPersonMiles::getMiles, bo.getMiles());
        lqw.between(params.get("beginDataTime") != null && params.get("endDataTime") != null,
            ReportPersonMiles::getDataTime ,params.get("beginDataTime"), params.get("endDataTime"));
        lqw.eq(bo.getWorkTime() != null, ReportPersonMiles::getWorkTime, bo.getWorkTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(ReportPersonMilesBo bo) {
        ReportPersonMiles add = BeanUtil.toBean(bo, ReportPersonMiles.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setPersonMilesId(add.getPersonMilesId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(ReportPersonMilesBo bo) {
        ReportPersonMiles update = BeanUtil.toBean(bo, ReportPersonMiles.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportPersonMiles entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
